﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HitachiLib.Utils;
using HitachiLib.Data;
using HitachiLib.HitachiServiceRef;
using HitachiApp.Model.Projects;
using HitachiApp.Model.Resources;

namespace HitachiApp.Model.Manage
{
    public class ProjectResourceRelationControlModel : INotifyPropertyChanged
    {
        public ProjectResourceRelationControlModel(ManageViewModel parent)
        {
            manage_view_model = parent;
            project_read = new AccessToggleControlModel(AccessToggleControlModel.ProjectReadAccess, "设备读", relation, this);
            project_write = new AccessToggleControlModel(AccessToggleControlModel.ProjectWriteAccess, "设备写", relation, this);
            resource_read = new AccessToggleControlModel(AccessToggleControlModel.ResourceReadAccess, "人员读", relation, this);
            resource_write = new AccessToggleControlModel(AccessToggleControlModel.ResourceWriteAccess, "人员写", relation, this);
            schedule_read = new AccessToggleControlModel(AccessToggleControlModel.ScheduleReadAccess, "排班读", relation, this);
            schedule_write = new AccessToggleControlModel(AccessToggleControlModel.ScheduleWriteAccess, "排班写", relation, this);
        }

        private ManageViewModel manage_view_model;
        ProjectResourceRelation relation = null;
        ProjectManageItem project;
        ResourceManageItem resource;

        public Visibility Loaded
        {
            get {
                if (relation != null)
                    return Visibility.Visible;
                else
                    return Visibility.Collapsed;
            }
        }
        public ProjectManageItem Project
        {
            get { return project; }
            set { project = value; }
        }
        public ResourceManageItem Resource
        {
            get { return resource; }
            set { resource = value; }
        }
        public ProjectResourceRelation Relation
        {
            get { return relation; }
            set
            {
                relation = value;
                RaisePropertyChanged("Loaded");
                project_read.Relation = relation;
                project_write.Relation = relation;
                resource_read.Relation = relation;
                resource_write.Relation = relation;
                schedule_read.Relation = relation;
                schedule_write.Relation = relation;
                Update();
            }
        }

        public void Update()
        {
            RaisePropertyChanged("Relation");
            RaisePropertyChanged("ProjectRead");
            RaisePropertyChanged("ProjectWrite");
            RaisePropertyChanged("ResourceRead");
            RaisePropertyChanged("ResourceWrite");
            RaisePropertyChanged("ScheduleRead");
            RaisePropertyChanged("ScheduleWrite");
        }

        public void Refresh()
        {
            if (project != null && resource != null)
            {
                if (relation == null)
                {
                    ProjectResourceData prd = new ProjectResourceData();
                    prd.ProjectID = project.Data.Id;
                    prd.ResourceID = resource.Data.Id;
                    prd.RelationFlag = 0;
                    relation = new ProjectResourceRelation(prd);
                }
            }
            else
            {
                relation = null;
            }
            project_read.Relation = relation;
            project_write.Relation = relation;
            resource_read.Relation = relation;
            resource_write.Relation = relation;
            schedule_read.Relation = relation;
            schedule_write.Relation = relation;
            Update();
        }

        public void Save()
        {
            HitachiServiceClient client = WCFClient.GetClient();
            client.SaveProjectResourceCompleted += new EventHandler<SaveProjectResourceCompletedEventArgs>(client_SaveProjectResourceCompleted);
            WCFClient.Retain();
            client.SaveProjectResourceAsync(Relation.Data);
        }

        void client_SaveProjectResourceCompleted(object sender, SaveProjectResourceCompletedEventArgs e)
        {
            WCFClient.Release();
            ProjectResourceData prd = e.Result;
            if (prd.ID == 0)
            {
                manage_view_model.RemoveRelation(prd);
            }
            else
            {
                manage_view_model.AddRelation(prd);
            }
        }

        AccessToggleControlModel project_read;
        AccessToggleControlModel project_write;
        AccessToggleControlModel resource_read;
        AccessToggleControlModel resource_write;
        AccessToggleControlModel schedule_read;
        AccessToggleControlModel schedule_write;

        public AccessToggleControlModel ProjectRead
        {
            get { return project_read; }
        }
        public AccessToggleControlModel ProjectWrite
        {
            get { return project_write; }
        }
        public AccessToggleControlModel ResourceRead
        {
            get { return resource_read; }
        }
        public AccessToggleControlModel ResourceWrite
        {
            get { return resource_write; }
        }
        public AccessToggleControlModel ScheduleRead
        {
            get { return schedule_read; }
        }
        public AccessToggleControlModel ScheduleWrite
        {
            get { return schedule_write; }
        }

        #region PropertChanged Block
        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
        #endregion
    }
}
